function LoginView(controller) {
	View.call(this, controller);
	
	this.addInputs = function() {
		var logoutTr = document.createElement("tr");
		  var loggedAsTd = document.createElement("td");
		    loggedAsTd.colSpan = 2;
		    loggedAsTd.className = "loggedAs";
		    this.loggedAs = document.createElement("span");
		    loggedAsTd.appendChild(this.loggedAs);
		  logoutTr.appendChild(loggedAsTd);
		this.tbody.appendChild(logoutTr);
		
		var loginTr = document.createElement("tr");
		  var loginTdText = document.createElement("td");
		    loginTdText.className = "login";
		    var loginTextNode = document.createTextNode("Login:");
		    loginTdText.appendChild(loginTextNode);
		  loginTr.appendChild(loginTdText);
		  var loginTd = document.createElement("td");
		  	this.loginInput = document.createElement("input");
		  	  this.loginInput.className = "login";
		  	  this.loginInput.type = "text";
		  	this.loginInput.onkeypress = (function(obj, method) {
		  		return function(event) {
		  			var keyCode = (event.keyCode) ? event.keyCode : event.which;
		  			eval("obj." + method + "(keyCode);");
		  		};
		  	})(this, "onKeyPress");
		  	loginTd.appendChild(this.loginInput);
		  loginTr.appendChild(loginTd);
		this.tbody.appendChild(loginTr);
		
		var passwordTr = document.createElement("tr");
		  var passwordTdText = document.createElement("td");
		    passwordTdText.className = "password";
	        var passwordTextNode = document.createTextNode("Password:");
	        passwordTdText.appendChild(passwordTextNode);
	      passwordTr.appendChild(passwordTdText);
		  var passwordTd = document.createElement("td");
		    this.passwordInput = document.createElement("input");
		      this.passwordInput.type = "password";
		      this.passwordInput.className = "password";
		      this.passwordInput.onkeypress = (function(obj, method) {
			  		return function(event) {
			  			var keyCode = (event.keyCode) ? event.keyCode : event.which;
			  			eval("obj." + method + "(keyCode);");
			  		};
			  	})(this, "onKeyPress");
		    passwordTd.appendChild(this.passwordInput);
		  passwordTr.appendChild(passwordTd);
		this.tbody.appendChild(passwordTr);

		var logButtonsTr = document.createElement("tr");
		  loginButtonTd = document.createElement("td");
		    loginButtonTd.className = "login-button";
		    this.loginButton = document.createElement("input");
		      this.loginButton.type = "button";
		      this.loginButton.value = "Login";
		      this.loginButton.className = "login-button";
		      this.loginButton.onclick = (function(obj, method) {
			      return function() {
				      eval("obj." + method + ";");
				  };
		      })(this, "login()");
		    loginButtonTd.appendChild(this.loginButton);
		  logButtonsTr.appendChild(loginButtonTd);

		  var logoutTd = document.createElement("td");
		    logoutTd.className = "logout-button";
		    this.logoutButton = document.createElement("input");
	          this.logoutButton.type = "button";
	          this.logoutButton.value = "Logout";
	          this.logoutButton.className = "logout-button";
	          this.logoutButton.onclick = (function(obj, method) {
		          return function() {
			          eval("obj." + method + "();");
			      };
	          })(this, "logout");
	        logoutTd.appendChild(this.logoutButton);
		  logButtonsTr.appendChild(logoutTd);
		this.tbody.appendChild(logButtonsTr);
	};
	
	this.onKeyPress = function(keyCode) {
		if (13 == keyCode) {
			this.login();
		}
	};
	
	this.getEncriptedPassword = function(publicKey) {
		var baseString = MD5(this.passwordInput.value) + publicKey;
		return MD5(baseString);
	};
	
	this.login = function() {
		var controller = this.getController();
		var publicKey = controller.getId();
		var encriptedPassword = this.getEncriptedPassword(publicKey);
		
		var params = {"login" : this.loginInput.value, "password" : encriptedPassword};
		controller.callMethod([this, "loginDone"], "login", params);
	};
	
	this.loginDone = function(user) {
		this.loginInput.value = "";
		this.passwordInput.value = "";

		if (user) {
			this.setLoggedUser(user);
		} else {
			alert("Los datos de acceso son incorrectos");
		}
	};

	this.updateLoggedUser = function() {
		this.controller.callMethod([this, "setLoggedUser"], "getLoggedUser", {});
	};
	
	this.setLoggedUser = function(user) {
		this.user = user;

		while(this.loggedAs.hasChildNodes()) {
			this.loggedAs.removeChild(this.loggedAs.firstChild);
		}

		var loggedAsTextNode = null;
		if (this.logged()) {
			loggedAsTextNode = document.createTextNode("Logged as: " + this.user.login);
		} else {
			loggedAsTextNode = document.createTextNode("Not Logged");
		}

		this.loggedAs.appendChild(loggedAsTextNode);
	};
	
	this.logged = function() {
		return (this.user ? true : false);
	};
	
	this.logout = function() {
		this.controller.callMethod([this, "setLoggedUser"], "logout", {});
	};

	// ////////////////////////////////////////////////
	this.tag = document.createElement("table");
//	this.tag.border = 1;
	this.tag.className = "LoginView";
	this.tbody = document.createElement("tbody");
	this.tag.appendChild(this.tbody);
	// ////////////////////////////////////////////////
	
	this.loggedAs = null;
	this.logoutButton = null;
	this.loginInput = null;
	this.passwordInput = null;
	this.loginButton = null;
	
	this.user = false;
	
	this.addInputs();
	this.updateLoggedUser();
}